package com.zhu.nio.channel;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by ehsy_it on 2016/4/12.
 */
public class GatheringWrite {

    public static void main(String[] args) throws IOException {
        RandomAccessFile file = new RandomAccessFile("d:/data/gather-write-data.txt", "rw");
        FileChannel channel = file.getChannel();

        ByteBuffer buf1 = ByteBuffer.allocate(4);
        ByteBuffer buf2 = ByteBuffer.allocate(5);
        ByteBuffer buf3 = ByteBuffer.allocate(6);

        buf1.put("i am".getBytes());
        buf1.flip();
        buf2.put("a".getBytes());
        buf2.flip();
        buf3.put(" pig ".getBytes());
        buf3.flip();

        ByteBuffer[] byteArr = {buf1, buf2, buf3};

        channel.write(byteArr);

        file.close();
    }
}
